import Vue from 'vue'
import VueRouter from 'vue-router'
// 组件引入
import Login from '../components/Login.vue'
import Index from '../components/Index.vue'
import DataStatistics from '../components/dataStatistics/DataStatistics.vue'
import ExpressManagement from '../components/express/ExpressManagement.vue'
import Repository from '../components/repository/Repository.vue'
import ToExpress from '../components/express/ToExpress.vue'
import FeedBack from '../components/express/Feedback.vue'
import Announcement from '../components/message/Announcement.vue'
import Account from '../components/account/Account.vue'
Vue.use(VueRouter)
// Vue路由配置
const routes = [
  // 输入登录路劲访问登录模块
  {
    name: 'login',
    path: '/login',
    component: Login
  },
  // 默认访问重定向到登录模块
  {
    path: '/',
    component: Login
  },
  // 后台主页面
  {
    name: 'index',
    path: '/index',
    component: Index,
    redirect: '/dataStatistics',
    // 子路由
    children:[
      // 数据统计
      {
        path: '/dataStatistics',
        component: DataStatistics
      },
      // 快递入库信息管理
      {
        path: '/expressManagement',
        component: ExpressManagement
      },
      // 仓位管理
      {
        path: '/repository',
        component: Repository
      },
      // 寄件信息管理
      {
        path: '/toExpress',
        component: ToExpress
      },
      // 快递信息反馈
      {
        path: '/feedback',
        component: FeedBack
      },
      // 公告发布
      {
        path:'/announcement',
        component: Announcement
      },
      // 账户管理
      {
        path:'/account',
        component: Account
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 全局路由守卫
router.beforeEach((to,from,next)=>{
  console.log("开启路由守卫");
  let token = localStorage.getItem("token");
  if (to.path == '/login' || to.path == '/') {
    next();
  }else {
    if(token == null) {
      localStorage.removeItem("userName")
      alert("请先登录")
      next({
        name:'login'
      })
    }else{
      next();
    }
  }
})

export default router
